﻿using System;
using System.ComponentModel;
using INTEX.DataBase.Tools;
using ReflectionStudio.Core.Events;
using INTEX.DataBase;
using INTEX.DataBase.Tools;
using System;
using System.Data;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for EditEventWindow.xaml
    /// </summary>
    public partial class AB50PLAN : UserControl
    {
        MainViewModel MainViewModel;
        SETINTEXDB SetMaster
        {
            get
            {
                return MainViewModel.SetMaster;
            }
        }
        SETINTEXDB SetEnv
        {
            get
            {
                return MainViewModel.SetEnv;
            }
        }
        SETINTEXDB SetDetail
        {
            get
            {
                return MainViewModel.SetDetail;
            }
        } 

        ApplicationBase app
        {
            get
            {
                return ApplicationBase.Instance;
            }
        }
        public INTEXDBC INTEXDBC
        {
            get
            {
                return app.INTEXDBC;
            }
        }
        public INTEXBL INTEXBL
        {
            get
            {
                return app.INTEXBL;
            }
        }
        bool IsLoaded = false;
   
        CollectionViewSource vwTABAX
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwTABAX");
            }
        }
        DataRow dwTABAX
        {
            get
            {
                if (vwTABAX.View == null)
                    return null;
                if ((DataRowView)vwTABAX.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwTABAX.View.CurrentItem).Row;
            }
        }
        CollectionViewSource vwABAX
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwABAX");
            }
        }
        DataRow dwABAX
        {
            get
            {
                if (vwABAX.View == null)
                    return null;
                if ((DataRowView)vwABAX.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwABAX.View.CurrentItem).Row;
            }
        }
        BindingListCollectionView cvABAX
        {
            get
            {
                return (BindingListCollectionView)CollectionViewSource.GetDefaultView(dlgABAX.ItemsSource);
            }
        }
        public AB50PLAN()
        {
       
            InitializeComponent(); 
            this.Loaded += Windows_Loaded;
        }

        void Windows_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            if (IsLoaded)
                return;
            IsLoaded = true;
            MainViewModel = (MainViewModel)this.DataContext;
            vwABAX.View.CurrentChanged += vwABAX_CurrentChanged;
            cvABAX.CustomFilter = "ABAWAKZ='G' AND ABAAUFTYP='V'";
        }

        void vwABAX_CurrentChanged(object sender, EventArgs e)
        {
            try
            {
                if (dwABAX == null)
                    return;
                string filter = "ABAAUFTYP='V' AND ABAWAKZ='" + dwABAX["ABAWAKZ"].ToString() + "' AND ABAART='" + dwABAX["ABAART"].ToString() + "' AND ABAFARBE='" + dwABAX["ABAFARBE"].ToString() + "'";
                DataRow[] rows = SetMaster.ICC_ABAX.Select(filter);
                DataTable tb = rows.CopyToDataTable();
                tb.TableName = "ICC_ABAX";
                SetDetail.Clear();
                SetDetail.Merge(tb);
                double qty = 0;
                string dd = "";
                foreach (DataRow row in tb.Rows)
                {
                    qty += Convert.ToDouble(row["ABARESMG"].ToString()) / 1000;
                    dd = row["ABAVKENDD"].ToString();
                }
                txtTQty.Text = qty.ToString();
                txtDD.Text = dd;
                vwABAX.View.MoveCurrentTo(-1);
                if (vwTABAX.View != null)
                    vwTABAX.View.MoveCurrentToFirst();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void btnCombine_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (dwTABAX == null)
                {
                    MessageBox.Show("请先选择要合并的生产单");
                    return;
                }
                double RQTY = Convert.ToDouble(txtTQty.Text.ToString()) * 100;
                DataTable tbICC_ABAX = SetDetail.Tables["ICC_ABAX"].Copy();
                tbICC_ABAX.TableName = "ICC_ABAX";
                SetDetail.Clear();
                SetDetail.Merge(SetMaster);
                SetDetail.Tables["ICC_ABAX"].Clear();
                SetDetail.Merge(tbICC_ABAX);
                DataSet SReult = MainViewModel.Combine(SetEnv, SetDetail, RQTY, txtDD.Text.ToString());

                string filter = "ABAWAKZ='" + dwABAX["ABAWAKZ"].ToString() + "' AND ABAART='" + dwABAX["ABAART"].ToString() + "' AND ABAFARBE='" + dwABAX["ABAFARBE"].ToString() + "'";
                DataRow[] rows = SetMaster.ICC_ABAX.Select(filter);
                foreach (DataRow row in rows)
                {
                    row.Delete();
                }
                SetMaster.AcceptChanges();
                filter = "ABAAUFTYP='V'";
                rows = SReult.Tables["ICC_ABAX"].Select(filter);
                DataTable tb = rows.CopyToDataTable();
                tb.TableName = "ICC_ABAX";
                SetMaster.Merge(tb);
                SetDetail.Clear();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void btnSplitBatch_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (dwTABAX == null)
                {
                    MessageBox.Show("请先选择要拆分的生产单");
                    return;
                }
                string filter = "ABAID<>'" + dwTABAX["ABAID"].ToString() + "'";
                DataRow[] rows = SetDetail.Tables["ICC_ABAX"].Select(filter);
                foreach (DataRow row in rows)
                {
                    row.Delete();
                }
                SetDetail.AcceptChanges();
                DataTable tbICC_ABAX = SetDetail.Tables["ICC_ABAX"].Copy();
                tbICC_ABAX.TableName = "ICC_ABAX";
                SetDetail.Clear();
                SetDetail.Merge(SetMaster);
                SetDetail.Tables["ICC_ABAX"].Clear();
                SetDetail.Merge(tbICC_ABAX);
                double RQTY = Convert.ToDouble(txtTQty.Text.ToString());
                DataSet SReult = MainViewModel.SplitAB(SetEnv, SetDetail, RQTY, txtDD.Text.ToString());
                filter = "ABAWAKZ='" + dwABAX["ABAWAKZ"].ToString() + "' AND ABAART='" + dwABAX["ABAART"].ToString() + "' AND ABAFARBE='" + dwABAX["ABAFARBE"].ToString() + "'";
                rows = SetMaster.ICC_ABAX.Select(filter);
                foreach (DataRow row in rows)
                {
                    row.Delete();
                }
                SetMaster.AcceptChanges();
                filter = "ABAAUFTYP='V'";
                rows = SReult.Tables["ICC_ABAX"].Select(filter);
                DataTable tb = rows.CopyToDataTable();
                tb.TableName = "ICC_ABAX";
                SetMaster.Merge(tb);
                SetDetail.Clear();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void btnReloadABAX_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                /*
                btnQuery_Click(null, null);
                return;
                SetMaster.Clear();
                SetDetail.Clear();
                DataRow dwData = SetMaster.ICC_QUERYTB.NewRow();
                dwData["ABAMANDANT"] = app.Mandant;
                dwData["EXART"] = Helpers.Helper.Left((KRS + "-" + Helpers.Helper.Right(LFD, 4)).PadRight(20), 20);
                SetMaster.Merge(INTEXDBC.QueryICC_ABAX(dwData, 3));*/
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void btnRemoveFromCombine_Click(object sender, RoutedEventArgs e)
        {
            if (dwTABAX == null)
                return;
            string ABAID = dwTABAX["ABAID"].ToString();
            DataRow[] rows=SetDetail.Tables["ICC_ABAX"].Select("ABAID='"+ABAID+"'");
            foreach (DataRow row in rows)
            {
                row.Delete();
            }
            SetDetail.AcceptChanges();
        }
    }
}
